package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCancelService;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderDeleteService;
import com.mall.order.OrderQueryService;
import com.mall.order.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shopping")
@Slf4j
public class OrderController {
    //status 0未付款 1已付款 2未发货 3已发货 4交易成功 5交易关闭 6交易失败 7-已退款
    //status 1库存已锁定 2库存已释放 3-库存减扣成功
    @Reference(check = false)
    OrderCoreService orderCoreService;
    @Reference(check = false)
    OrderQueryService orderQueryService;
    @Reference(timeout = 3000,check = false)
    OrderDeleteService orderDeleteService;
    @Reference(timeout = 3000,check = false)
    OrderCancelService orderCancelService;

    @GetMapping("/order")
    public ResponseData getAllOrders(Integer page, Integer size, HttpServletRequest servletRequest) {
        //获取用户信息
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSONObject.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("id").toString());
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        orderListRequest.setPage(page);
        orderListRequest.setSize(size);
        orderListRequest.setSort("create_time desc");
        GetOrderListDto response = orderQueryService.getUserOrders(orderListRequest);

        if (response != null) {
            ResponseData responseData = new ResponseUtil().setData(response);
            return responseData;
        }
        return new ResponseUtil().setErrorMsg("exception");
    }

    @DeleteMapping("order/{OrderId}")
    public ResponseData deleteOrder(@PathVariable("OrderId") String OrderId){
        OrderDeleteRequest request = new OrderDeleteRequest();
        request.setOrderId(OrderId);
        OrderDeleteResponse response = orderDeleteService.deleteOrder(request);
        return new ResponseUtil<>().setData(response);
    }

    /**
     *  取消订单
     * @param cancelOrderRequest
     * @return
     */
    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {

        ResponseUtil<Object> responseUtil = new ResponseUtil<>();
        CancelOrderResponse cancelOrderResponse = orderCancelService.cancelOrder(cancelOrderRequest);
        if (cancelOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return responseUtil.setData(cancelOrderResponse.getMsg());
        }
        return responseUtil.setErrorMsg(cancelOrderResponse.getMsg());
    }
}
